package com.slg.android.manyanime;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.adapter.CartoonCommonListAdapter;
import com.slg.android.manyanime.bean.CartoonNewest;
import com.slg.android.manyanime.utils.ApiUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;


public class CartoonCategoryDetailsActivity extends FragmentActivity implements PullToRefreshListView.OnRefreshListener2
 ,ListView.OnItemClickListener
{


    @ViewInject(R.id.cartoon_category_details_title_back)
    private ImageView backImg;

    @ViewInject(R.id.cartoon_category_details_title_search)
    private ImageView searchImg;

    @ViewInject(R.id.cartoon_category_details_title_text)
    private TextView categoryName;

    @ViewInject(R.id.cartoon_category_details_list)
    private PullToRefreshListView refreshListView;

    private List<CartoonNewest> beans;
    private int cateid;
    private CartoonCommonListAdapter adapter;

    int currentPage = 1;
    private ListView trueList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_cartoon_category_details);
        ViewUtils.inject(this);
        Intent intent = getIntent();

        cateid = intent.getIntExtra("cateId", 0);
        String groupName = intent.getStringExtra("cateName");

        refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        refreshListView.setOnRefreshListener(this);

        trueList = refreshListView.getRefreshableView();
        if (groupName != null) {
            categoryName.setText(groupName);
        }

        beans = new LinkedList<>();
        if (cateid != 0) {
            adapter = new CartoonCommonListAdapter(this, beans);
            trueList.setAdapter(adapter);
        }
        parseData(ApiUtils.CARTOON_CATEGORY_DETAILS + cateid);

        trueList.setOnItemClickListener(this);

    }


    public void parseData(String url) {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpRequest.HttpMethod.GET,
                url,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                        String str = stringResponseInfo.result;
                        if (str != null) {
                            try {
                                JSONObject object = new JSONObject(str);
                                JSONArray list = object.getJSONArray("list");
                                for (int i = 0; i < list.length(); i++) {
                                    CartoonNewest cartoonNewest = new CartoonNewest();
                                    cartoonNewest.parseJson(list.getJSONObject(i));

                                    if (!beans.contains(cartoonNewest)) {
                                        beans.add(cartoonNewest);
                                    }
                                }
                                adapter.notifyDataSetChanged();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(CartoonCategoryDetailsActivity.this,
                                R.string.no_network, Toast.LENGTH_LONG).show();
                    }
                }
        );
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        String url = ApiUtils.CARTOON_CATEGORY_DETAILS + cateid;
        parseData(url);
        refreshListView.onRefreshComplete();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        currentPage++;
        String url = ApiUtils.CARTOON_CATEGORY_DETAILS + cateid + "&page=" + currentPage;
        parseData(url);
        refreshListView.onRefreshComplete();


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int truePostion = position - trueList.getHeaderViewsCount();
        Intent intent=new Intent(this,CartoonDetailActivity.class);
        intent.putExtra("id",beans.get(truePostion).getId());
        startActivity(intent);
    }
}
